//
//  CZKeyboardToolbar.swift
//  weibo
//
//  Created by 丁蒙 on 16/7/4.
//  Copyright © 2016年 丁蒙. All rights reserved.
//

import UIKit
//按钮 枚举
enum CZKeyboardToolbarType: Int {
    case Default = 0
    case Emoji = 1
    case Lxh = 2
}

let num: Int = 1000


class CZKeyboardToolbar: UIView {
    //点击按钮 闭包
    var emoticonClosure:((type: CZKeyboardToolbarType)->())?
    //选中按钮
    var currentButton: UIButton?

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK: - 设置视图
    private func setupUI() {
        //添加子控件
        let defaultButton = addChildButtons("left", title: "默认",type: .Default)
        let emojiButton = addChildButtons("mid", title: "Emoji",type: .Emoji)
        let lxhButton = addChildButtons("right", title: "浪小花",type: .Lxh)
        
        //添加约束
        defaultButton.snp_makeConstraints { (make) in
            make.leading.bottom.top.equalTo(self)
            make.width.equalTo(emojiButton)
        }
        emojiButton.snp_makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(defaultButton.snp_trailing)
            make.width.equalTo(lxhButton)
        }
        lxhButton.snp_makeConstraints { (make) in
            make.leading.equalTo(emojiButton.snp_trailing)
            make.top.bottom.trailing.equalTo(self)
        }
        //设置默认选中按钮
        defaultButton.selected = true
        currentButton = defaultButton
        
    }
    // MARK: - 监听按钮点击事件
    @objc private func buttonClick(button: UIButton) {
        //执行闭包
        emoticonClosure?(type: CZKeyboardToolbarType(rawValue: button.tag - num)!)
        
        currentButton?.selected = false
        button.selected = true
        currentButton = button
    }
    // MARK: - 创建按钮的公用方法
    private func addChildButtons(imageName: String,title: String,type: CZKeyboardToolbarType)-> UIButton {
        let btn: UIButton = UIButton()
        
        btn.tag = type.rawValue + num
        
        btn.setBackgroundImage(UIImage(named: "compose_emotion_table_\(imageName)_normal"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "compose_emotion_table_\(imageName)_selected"), forState: UIControlState.Selected)
        
         btn.setTitle(title, forState: UIControlState.Normal)
        
        btn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Selected)
        
        btn.addTarget(self, action: #selector(CZKeyboardToolbar.buttonClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        
        addSubview(btn)
        
        return btn
    }
    // MARK: - 供外界调用的方法
    func setCurrentButtons(tag: Int) {
        
        let button = viewWithTag(tag + num) as! UIButton
        
        if currentButton == button {
            return
        }
        
        button.selected = true
        currentButton?.selected = false
        currentButton = button
    }
    
}
